package com.hcrazy.manchester.app.services;

import java.util.List;

import org.joda.time.DateTime;

import com.hcrazy.manchester.app.jdbc.RadioChannelDAO;
import com.hcrazy.manchester.app.jdbc.RadioTagDAO;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.RadioChannel;
import com.hcrazy.manchester.app.pojo.RadioTag;
import com.hcrazy.manchester.app.utils.Constant;

public class RadioChannelService {

	private RadioChannelDAO radioChannelDAO;
	
	private RadioTagDAO radioTagDAO;

	public RadioTagDAO getRadioTagDAO() {
		return radioTagDAO;
	}

	public void setRadioTagDAO(RadioTagDAO radioTagDAO) {
		this.radioTagDAO = radioTagDAO;
	}

	public RadioChannelDAO getRadioChannelDAO() {
		return radioChannelDAO;
	}

	public void setRadioChannelDAO(RadioChannelDAO radioChannelDAO) {
		this.radioChannelDAO = radioChannelDAO;
	}

	public ErrorMsg addOrUpdateRadioChannel(RadioChannel radioChannel) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		
		radioChannel.setCreateDate(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
		
		if(radioChannel.getChannelId() > 0){
			radioChannel.setPraiseCount(radioChannel.getPraiseCount() + radioChannel.getPraiseBaseCount());
			radioChannel.setCommentCount(radioChannel.getCommentCount() + radioChannel.getCommentBaseCount());
			
			radioChannelDAO.updateRadioChannel(radioChannel);
			
		}else{
			if(null != radioChannel && radioChannel.getPraiseBaseCount() > 0){
				radioChannel.setPraiseCount(radioChannel.getPraiseBaseCount());
			}
			if(null != radioChannel && radioChannel.getCommentBaseCount() > 0){
				radioChannel.setCommentCount(radioChannel.getCommentBaseCount());
			}
			
			radioChannelDAO.addRadioChannel(radioChannel);
		}
		
		return msg;
	}
	
	private String setNames(String typeName,RadioTag tag){
		String names = "";
		if(typeName != null){
			if((typeName.equals(tag.getTagId()+"") || typeName.endsWith(","+tag.getTagId())
					|| typeName.startsWith((tag.getTagId()+",")) || typeName.contains(","+tag.getTagId()+","))){
				
				names = tag.getTagName();
			}
		}
		return names;
	}

	public PageModel getAddressInfos(PageModel model,String search,int cType,int cScene,int cStyle) throws Exception{
		
		PageModel m = radioChannelDAO.getPagedInfos(model, search, cType, cScene, cStyle);
		if(null != m && null != m.getResult() && m.getResult().size()>0){
			//获取全部的场景风格等
			List<RadioTag> tags = radioTagDAO.listAllSorts(Constant.RADIO_TAG_RADIO);
			
			for(Object obj:m.getResult()){
				RadioChannel channel = (RadioChannel)obj;
				String cTypeName = "";
				String cSceneName = "";
				String cStyleName = "";
				for(RadioTag tag:tags){
					String a1= setNames(channel.getcType(),tag);
					String a2 = setNames(channel.getcScene(),tag);
					String a3 = setNames(channel.getcStyle(),tag);
					if(!a1.equals("")){
						cTypeName += (cTypeName.equals("")) ?setNames(channel.getcType(),tag):","+setNames(channel.getcType(),tag);
					}
					if(!a2.equals("")){
						cSceneName += (cSceneName.equals(""))?setNames(channel.getcScene(),tag):","+setNames(channel.getcScene(),tag);
					}
					if(!a3.equals("")){
						cStyleName += (cStyleName.equals(""))?setNames(channel.getcStyle(),tag):","+setNames(channel.getcStyle(),tag);
					}
				}
				
				channel.setcTypeName(cTypeName);
				channel.setcSceneName(cSceneName);
				channel.setcStyleName(cStyleName);
			}
		}
		return m;
	}
	
	public RadioChannel getInfoById(int infoId) throws Exception{
		RadioChannel radioChannel = radioChannelDAO.getById(infoId);
		return radioChannel;
	}
	
	public void delRadioChannel(int infoId) throws Exception{
		radioChannelDAO.delRadioChannel(infoId);
	}
	
	public String generateNum() throws Exception{
		int count = radioChannelDAO.getCount();
		String fmNum = "FM";
		DateTime dt = new DateTime();
		fmNum += dt.toString("yyyyMMdd") + count;
		
		return fmNum;
	}
	
	public List<RadioChannel> listAllInfos() throws Exception{
		return radioChannelDAO.listAllInfos();
	}
	
	public List<RadioChannel> getAllInfosBySortId(int cType,int cScene,int cStyle,String search) throws Exception{
		return radioChannelDAO.getAllInfosBySortId(cType, cScene, cStyle, search);
	}
}
